using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using EntLibEC.Info.Online;

using EntLibEC.Biz;
using EntLibEC.Biz.Online;

using EntLibEC.Cmn;



namespace EntLibEC.View.Online
{
	/// <summary>
	/// Summary description for PromotionDistribute.
	/// </summary>
	public partial class PromotionDistribute : PageBase
	{
		private int sysno;
	
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			if (Request.QueryString["SysNo"]!=null)
			{
				try
				{
					this.sysno = Convert.ToInt32(Request.QueryString["sysno"].ToString());
				}
				catch
				{
					Assert(lblMsg, "sysno is not an integer", -1);					
					return;
				}
			}
			else
			{
				Assert(lblMsg,"sysno error !",-1);				
				return ;
			}

			if(!Page.IsPostBack)
			{				
				bindForm();
			}
		}

		private void bindForm()
		{
		    PromotionCodeInfo oInfo = PromotionManager.GetInstance().LoadPromotionCode(sysno);
			if(oInfo == null)
			{
			    Assert(lblMsg , "Parameter Error!" , -1);
				return;
			}
			
			lblPromotionCode.Text = oInfo.PromotionCode;
			lblPromotionValue.Text = Util.TrimNull(oInfo.PromotionValue);
			lblSoAmtLimit.Text = Util.TrimNull(oInfo.SoAmtLimit);
			lblStatus.Text = AppEnum.GetBiStatus(oInfo.Status);
			lblTotalCount.Text = Util.TrimNull(oInfo.TotalCount);
			lblUseCount.Text = Util.TrimNull(oInfo.UseCount);	
		
			bindGrid();

		}

		private void bindGrid()
		{
			Hashtable ht = new Hashtable();
			ht.Add("PromotionCodeSysNo" , sysno);
			DataSet ds = PromotionManager.GetInstance().GetPromotionCustomerList(ht);
			ds = convertDs(ds);
			base.SetDataGridPage(ds, dgPromotionCustomer);
			dgPromotionCustomer.DataSource = ds;
			dgPromotionCustomer.DataBind();	
			Assert(lblMsg , (ds.Tables[0].Rows.Count).ToString() + " Record(s)!" , 1);
		}

		private DataSet convertDs(DataSet ds)
		{
		    if(!Util.HasMoreRow(ds))
				return ds;

			ds.Tables[0].Columns.Add("SOID");

			foreach(DataRow dr in ds.Tables[0].Rows)
			{
			    dr["SOID"] = "<a href=\"javascript:openWindowL('../Sale/SODetail.aspx?SysNo="+dr["SOSysNo"].ToString()+"')\">"+dr["SoSysNo"]+"</a>";
			}

			return ds;
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.dgPromotionCustomer.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgPromotionCustomer_ItemCommand);
			this.dgPromotionCustomer.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dgPromotionCustomer_PageIndexChanged);

		}
		#endregion

		protected void btnOk_Click(object sender, System.EventArgs e)
		{
			if ( !base.CheckAccess((int)AppEnum.Privilege.PromotionCodeManage, false, lblMsg) )
				return;
			if(txtCustomer.Text == AppConst.StringNull)
			{
			    Assert(lblMsg , "Please Input CustomerSysNo!" , -1);
				return;				
			}
			try
			{
				PromotionManager.GetInstance().DistributePromotionCode(sysno , txtCustomer.Text.Trim());

				bindGrid();
			    Assert(lblMsg , "Distribute PromotionCode to Customer Successfully!!" , 1);
			}   
			catch(BizException biz)
			{
				Assert(lblMsg , biz.Message , -1);
				return;
			}
			catch
			{
			    Assert(lblMsg , "Distribute PromotionCode to Customer Error!",-1);
				return;
			}
		}

		private void dgPromotionCustomer_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			if(e.CommandName == "Delete")
			{
				try
				{
					if ( !base.CheckAccess((int)AppEnum.Privilege.PromotionCodeManage, false, lblMsg) )
						return;
		            PromotionManager.GetInstance().DeletePromotionCustomer((int)dgPromotionCustomer.DataKeys[(int)e.Item.ItemIndex])  ;
					bindGrid();
					Assert(lblMsg , "Delete Promotion Customer Successfully!" , 1);
				}
				catch
				{
					Assert(lblMsg , "Delete Promotion Customer Error!" , -1);
				}
			}
		}

		private void dgPromotionCustomer_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
		{
			dgPromotionCustomer.CurrentPageIndex = e.NewPageIndex;
			bindGrid();
		}
	}
}
